<template>
  <div>
    <div class="base_header" style="text-align: left;margin-bottom: 20px">
      <span style="font-size: 30px;font-weight: normal;">
        路径管理
      </span>
      <span style="font-size: 10px">网站路径管理</span>

    </div>
    <div>
      <el-collapse>
        <el-collapse-item  name="1">
          <template slot="title">
            <div style="padding: 20px;">
              <span style="font-size: 20px;font-weight: bold">
                功能面板
              </span>
              <span style="font-size: 14px;color: gray">
                (点击查看详情)
              </span>
            </div>
          </template>
          <div style="padding: 20px">
            <div class="el-col-20" style="">
              <el-form label-position="left">
                <el-form-item label="组名">
                  <el-select v-model="selectGroupId">
                    <el-option
                      label="全部"
                      value="0">
                    </el-option>
                    <el-option
                      v-for="item in groupList"
                      :label="item.title"
                      :value="item.groupId"
                      :key="item.groupId"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-form>
            </div>
            <div class="el-col-4" style="padding-left: 20px;border-left: 1px solid #f1f1f1;">
              <div>
                <el-button
                  type="primary"
                  @click="toGet"
                >查询
                </el-button>
                <el-button type="success"
                           style="margin-left: 10px"
                           @click="dialogVisible = true"
                >新增</el-button>
              </div>
              <div>
              </div>
          </div>
          </div>
        </el-collapse-item>
      </el-collapse>
    </div>
    <!--新增路径--Start-->
    <el-dialog
      title="新增路径"
      :visible.sync="dialogVisible"
      width="30%">
      <el-form label-position="left" v-model="newPath" label-width="80px">
        <el-form-item label="URL">
          <el-input v-model="newPath.path" placeholder="例：/index">
          </el-input>
        </el-form-item>
        <el-form-item label="显示名称">
          <el-input v-model="newPath.name" placeholder="例：首页">
          </el-input>
        </el-form-item>
        <el-form-item label="分组">
            <el-select v-model="newPath.groupId">
              <el-option
                v-for="item in groupList"
                :label="item.title"
                :value="item.groupId"
                :key="item.groupId"
              ></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="父路径">
          <el-select v-model="newPath.parentId" filterable>
            <el-option
              v-for="item in allPath"
              :label="item.name"
              :value="item.id"
              :key="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="等级">
          <el-select v-model="newPath.level">
            <el-option label="1" value="1"></el-option>
            <el-option label="2" value="2"></el-option>
            <el-option label="3" value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="newPath.remark" placeholder="例：首页">
          </el-input>
        </el-form-item>
        <el-form-item label="对应图标">
          <el-input v-model="newPath.icon" placeholder="例：el-icon-setting">
          </el-input>
        </el-form-item>
      </el-form>
      <div style="text-align: right">
        <el-button @click="addCancle">取 消</el-button>
        <el-button type="primary" @click="addSave">确 定</el-button>
      </div>
    </el-dialog>
    <!--新增路径--End-->
    <el-table
      :data="pathList"
      style="width: 100%">
      <el-table-column
        label="序号"
        width="80">
        <template slot-scope="scope">
          <span>
            {{scope.$index + 1 +  (page - 1) * pageSize}}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        label="路径">
        <template slot-scope="scope">
          <el-input v-model="scope.row.path" v-if="showEdit[scope.$index]"></el-input>
          <span v-else>
            {{scope.row.path}}
          </span>
        </template>
      </el-table-column>

      <el-table-column
        label="显示名称">
        <template slot-scope="scope">
          <el-input v-model="scope.row.name" v-if="showEdit[scope.$index]"></el-input>
          <span v-else>
            {{scope.row.name}}
          </span>
        </template>
      </el-table-column>

      <el-table-column
        label="分组">
        <template slot-scope="scope">
          <el-select v-model="scope.row.groupId"
                     @change="selectChange(scope.row)"
                     v-if="showEdit[scope.$index]">
            <el-option
              v-for="item in groupList"
              :label="item.title"
              :value="item.groupId"
              :key="item.groupId"
            ></el-option>
          </el-select>
          <span v-else>
            {{scope.row.title}}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        label="父路径">
        <template slot-scope="scope">
          <el-select v-model="scope.row.parentId"
                     filterable
                     @change="selectParentChange(scope.row)"
                     v-if="showEdit[scope.$index]">
            <el-option
              v-for="item in allPath"
              :label="item.name"
              :value="item.id"
              :key="item.id"
            ></el-option>
          </el-select>
          <span v-else>
            {{scope.row.parentName}}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        label="路径等级">
        <template slot-scope="scope">
          <el-select v-model="scope.row.level"
                     filterable
                     v-if="showEdit[scope.$index]">
            <el-option label="1" value="1"></el-option>
            <el-option label="2" value="2"></el-option>
            <el-option label="3" value="3"></el-option>
          </el-select>
          <span v-else>
            <span v-if="scope.row.level!=0">{{scope.row.level}}</span>
          </span>
        </template>
      </el-table-column>
      <el-table-column
        label="备注">
        <template slot-scope="scope">
          <el-input v-model="scope.row.remark" v-if="showEdit[scope.$index]"></el-input>
          <span v-else>
            {{scope.row.remark}}
          </span>
        </template>
      </el-table-column>

      <el-table-column
        label="对应图标">
        <template slot-scope="scope">
          <el-input v-model="scope.row.icon" v-if="showEdit[scope.$index]"></el-input>
          <span v-else>
            <i :class="scope.row.icon"></i>
          </span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <div v-if="!showEdit[scope.$index]">
            <el-popconfirm
              title="确定删除吗？"
              @onConfirm="handleDelete(scope.$index, scope.row)"
            >
              <el-button
                size="mini"
                slot="reference"
                type="danger"
              >删除</el-button>
            </el-popconfirm>
            <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          </div>
          <div v-else>
            <el-button
              size="mini"
              type="danger"
              @click="handleCancle(scope.$index, scope.row)">取消</el-button>
            <el-button
              size="mini"
              type="success"
              @click="handleSave(scope.$index, scope.row)">保存</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination" style="text-align: center">
      <el-pagination
        @current-change="handleCurrentChange"
        :page-size="pageSize"
        :hide-on-single-page="true"
        layout="prev, pager, next"
        :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: 'PathManage',
  data () {
    return {
      total: 0,
      linPath: {},
      page: 1,
      pageSize: 10,
      selectGroupId: '0',
      dialogVisible: false,
      newPath: {
        path: '',
        remark: '',
        name: '',
        icon: ''
      },
      groupList: [],
      pathList: [],
      showEdit: [],
      allPath: []
    }
  },
  mounted () {
    this.getAllPath()
    this.getSizePath()
    this.getListGroup()
    this.getListPath(1, this.pageSize)
  },
  methods: {

    getAllPath () {
      var _this = this
      this.$axios.post('/api/path/getAllPath')
        .then(function (response) {
          var result = response.data
          if (result.isSuccess) {
            _this.allPath = result.result
          }
        })
    },
    getListGroup () {
      var _this = this
      this.$axios.post('/api/path/getListGroup')
        .then(function (response) {
          var result = response.data
          if (result.isSuccess) {
            _this.groupList = result.result
          }
        })
    },
    getListPath (page, pageSize) {
      var _this = this
      var commonQueryData = {
        page: page,
        pageSize: pageSize,
        groupId: this.selectGroupId
      }
      this.$axios.post('/api/path/getListPath',
        this.$qs.stringify({
          queryData: JSON.stringify(commonQueryData)
        }))
        .then(function (response) {
          var result = response.data
          if (result.isSuccess) {
            _this.pathList = result.result
            var len = _this.pathList.length
            _this.showEdit.splice(0)
            for (let i = 0; i < len; i++) {
              _this.showEdit.push(false)
            }
          }
        })
    },
    getSizePath () {
      var _this = this

      var commonQueryData = {
        groupId: this.selectGroupId
      }
      this.$axios.post('/api/path/getSizePath',
        this.$qs.stringify({
          queryData: JSON.stringify(commonQueryData)
        }))
        .then(function (response) {
          var result = response.data
          if (result.isSuccess) {
            _this.total = parseInt(result.result)
          }
        })
    },
    // 下方分页点击
    handleCurrentChange (page) {
      this.page = page
      this.getListPath(page, this.pageSize)
    },
    handleEdit (index, row) {
      this.$set(this.showEdit, index, true)
      this.linPath = JSON.parse(JSON.stringify(row))
    },
    handleCancle (index, row) {
      this.pathList.splice(index, 1, this.linPath)
      this.$set(this.showEdit, index, false)
    },
    handleSave (index, row) {
      var _this = this
      // eslint-disable-next-line eqeqeq
      this.$set(this.showEdit, index, false)
      var d = JSON.parse(JSON.stringify(row))
      this.$delete(d, 'title')
      this.$delete(d, 'parentName')
      this.$delete(d, 'parentPath')

      this.$axios.post('/api/path/updatePath',
        this.$qs.stringify({
          pathData: JSON.stringify(d)
        })
      )
        .then(function (response) {
          var data = response.data
          if (data.isSuccess) {
            _this.$message({
              type: 'success',
              message: '更新成功！',
              offset: 50
            })
          } else {
            _this.$message({
              type: 'error',
              message: '更新失败，请重试！',
              offset: 50
            })
          }
        })
    },
    addCancle () {
      this.dialogVisible = false
    },
    addSave () {
      var _this = this
      this.$axios.post('/api/path/addPath',
        this.$qs.stringify({
          pathData: JSON.stringify(this.newPath)
        })
      ).then(function (response) {
        var data = response.data
        if (data.isSuccess) {
          _this.$message({
            type: 'success',
            message: '添加成功',
            offset: 50
          })
          _this.name = ''
          _this.dialogVisible = false
          _this.getListPath(_this.page, _this.pageSize)
        } else {
          _this.$message({
            type: 'error',
            message: '添加失败，请重试！',
            offset: 50
          })
        }
      }).catch(function (eachCallback) {
        _this.$message({
          type: 'error',
          message: '添加失败，请重试！',
          offset: 50
        })
      })
    },
    handleDelete (index, row) {
      var _this = this

      var d = JSON.parse(JSON.stringify(row))
      this.$delete(d, 'title')
      this.$delete(d, 'parentName')
      this.$delete(d, 'parentPath')
      this.$axios.post('/api/path/deletePath',
        this.$qs.stringify({
          pathData: JSON.stringify(d)
        })
      )
        .then(function (response) {
          var data = response.data
          if (data.isSuccess) {
            _this.$message({
              type: 'success',
              message: '删除成功',
              offset: 50
            })
            _this.getListPath(_this.page, _this.pageSize)
          } else {
            // eslint-disable-next-line eqeqeq
            if (data.code == 501) {
              _this.$message({
                type: 'error',
                message: '删除失败，该学习方向下还有所属分类，请您检查',
                offset: 50
              })
              // eslint-disable-next-line eqeqeq
            } else if (data.code == 502) {
              _this.$message({
                type: 'error',
                message: '删除失败，该学习方向下还有所属课程，请您检查',
                offset: 50
              })
            }
          }
        })
    },
    selectChange (row) {
      for (var i = 0; i < this.groupList.length; i++) {
        // eslint-disable-next-line eqeqeq
        if (this.groupList[i].groupId == row.groupId) {
          row.title = this.groupList[i].title
          break
        }
      }
    },
    selectParentChange (row) {
      for (var i = 0; i < this.allPath.length; i++) {
        // eslint-disable-next-line eqeqeq
        if (this.allPath[i].id == row.parentId) {
          row.parentName = this.allPath[i].name
          break
        }
      }
    },
    toGet () {
      this.getSizePath()
      this.getListPath(1, this.pageSize)
    }
  }
}
</script>

<style scoped>

</style>
